"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.User = void 0;
const Config_1 = require("../Config");
const GameUtils_1 = __importDefault(require("../game/GameUtils"));
const MySQLService_1 = __importDefault(require("../service/MySQLService"));
const Logger_1 = __importDefault(require("../utils/Logger"));
const HallMgr_1 = __importDefault(require("./HallMgr"));
const Room_1 = require("./Room");
class User {
    constructor(connection) {
        this.score = 100;
        this.is_onLine = true; //是否在线
        this.player = null; //对局中的玩家角色
        this.watcher = null; //观战者
        this.connection = connection;
        //收到消息
        connection.on('text', this.onMessage.bind(this));
        //关闭监听
        connection.on('close', this.onDropLine.bind(this));
        //掉线监听
        connection.on('error', this.onDropLine.bind(this));
    }
    /** 用户上线，接收用户名去绑定connection */
    userBind(obj) {
        this.username = obj.data.username;
        MySQLService_1.default.updateUserLoginState(this.username, 1);
        HallMgr_1.default.users.set(this.username, this);
        HallMgr_1.default.updateOnline();
        this.initSpendData();
    }
    /** 行动消耗数据初始化，以后平衡性调整只需要修改服务端 */
    initSpendData() {
        let data = {
            COST_BORN_BIG: Config_1.Config.COST_BORN_BIG,
            COST_BIG_MOVE: Config_1.Config.COST_BIG_MOVE,
            COST_BIG_ATTACK: Config_1.Config.COST_BIG_ATTACK,
            COST_BIG_EVOLVE_幽灵: Config_1.Config.COST_BIG_EVOLVE_幽灵,
            COST_幽灵_MOVE: Config_1.Config.COST_幽灵_MOVE,
            COST_幽灵_EVOLVE_恶灵: Config_1.Config.COST_幽灵_EVOLVE_恶灵,
            COST_恶灵_ATTACK: Config_1.Config.COST_恶灵_ATTACK,
            COST_恶灵_POSSESS: Config_1.Config.COST_恶灵_POSSESS,
            COST_BIG_EVOLVE_炮台: Config_1.Config.COST_BIG_EVOLVE_炮台,
            COST_炮台_IMPACT: Config_1.Config.COST_炮台_IMPACT,
            COST_炮台_EVOLVE_狙炮: Config_1.Config.COST_炮台_EVOLVE_狙炮,
            COST_狙炮_AIM: Config_1.Config.COST_狙炮_AIM,
            COST_BIG_EVOLVE_顽石: Config_1.Config.COST_BIG_EVOLVE_顽石,
            COST_BIG_EVOLVE_地灵: Config_1.Config.COST_BIG_EVOLVE_地灵,
            COST_地灵_move: Config_1.Config.COST_地灵_move,
            COST_地灵_抹平: Config_1.Config.COST_地灵_抹平,
            COST_地灵_占据: Config_1.Config.COST_地灵_占据,
            COST_BIG_EVOLVE_精灵: Config_1.Config.COST_BIG_EVOLVE_精灵,
            COST_精灵_赐福: Config_1.Config.COST_精灵_赐福,
            COST_精灵_庇护: Config_1.Config.COST_精灵_庇护,
            COST_BIG_EVOLVE_沙兵: Config_1.Config.COST_BIG_EVOLVE_沙兵,
            COST_沙兵_EVOLVE_沙皇: Config_1.Config.COST_沙兵_EVOLVE_沙皇,
            COST_BIG_EVOLVE_野猪: Config_1.Config.COST_BIG_EVOLVE_野猪,
            COST_野猪_IMPACT: Config_1.Config.COST_野猪_IMPACT,
            COST_BIG_EVOLVE_野马: Config_1.Config.COST_BIG_EVOLVE_野马,
            COST_野马_IMPACT: Config_1.Config.COST_野马_IMPACT,
            COST_BIG_EVOLVE_拳师: Config_1.Config.COST_BIG_EVOLVE_拳师,
            COST_拳师_移动: Config_1.Config.COST_拳师_移动,
            COST_拳师_怒威神拳: Config_1.Config.COST_拳师_怒威神拳,
            COST_拳师_爆裂火拳: Config_1.Config.COST_拳师_爆裂火拳,
            COST_BIG_EVOLVE_灵阵: Config_1.Config.COST_BIG_EVOLVE_灵阵,
            COST_灵阵_迷雾森林: Config_1.Config.COST_灵阵_迷雾森林,
            COST_灵阵_九曲黄河: Config_1.Config.COST_灵阵_九曲黄河,
            COST_灵阵_地狱杀伐: Config_1.Config.COST_灵阵_地狱杀伐,
            COST_BIG_EVOLVE_天谕: Config_1.Config.COST_BIG_EVOLVE_天谕,
            COST_天谕_明魂搜根: Config_1.Config.COST_天谕_明魂搜根,
            COST_天谕_天眼传真: Config_1.Config.COST_天谕_天眼传真,
            COST_天谕_神兵天降: Config_1.Config.COST_天谕_神兵天降,
            COST_BIG_EVOLVE_妖术: Config_1.Config.COST_BIG_EVOLVE_妖术,
            COST_妖术_绝命献祭: Config_1.Config.COST_妖术_绝命献祭,
            COST_妖术_玄冰禁锢: Config_1.Config.COST_妖术_玄冰禁锢,
            COST_妖术_瘟疫传播: Config_1.Config.COST_妖术_瘟疫传播,
            COST_BIG_EVOLVE_鼓舞: Config_1.Config.COST_BIG_EVOLVE_鼓舞,
            COST_鼓舞_嗜血狂战: Config_1.Config.COST_鼓舞_嗜血狂战,
            COST_鼓舞_乘风破浪: Config_1.Config.COST_鼓舞_乘风破浪,
            COST_鼓舞_生生不息: Config_1.Config.COST_鼓舞_生生不息,
            COST_鼓舞_常胜将军: Config_1.Config.COST_鼓舞_常胜将军
        };
        let obj = GameUtils_1.default.getObj("initSpendData", data, false);
        this.sendObj(obj);
    }
    /** 用户下线，修改登录状态 */
    onDropLine(code) {
        if (!this.is_onLine) {
            return;
        }
        this.is_onLine = false;
        HallMgr_1.default.users.delete(this.username);
        HallMgr_1.default.updateOnline();
        if (this.room != null) {
            if (this.room.state == Room_1.RoomState.MATCHING) {
                this.room.cancelMatch(this); //匹配中掉线
            }
            else if (this.room.state == Room_1.RoomState.GAMEING) {
                this.room.checkPlayersOnLineState(this); //游戏中掉线
            }
        }
        MySQLService_1.default.updateUserLoginState(this.username, 0);
        this.gc();
    }
    gc() {
        var _a, _b;
        this.connection = null;
        this.room = null;
        (_a = this.player) === null || _a === void 0 ? void 0 : _a.gc();
        (_b = this.watcher) === null || _b === void 0 ? void 0 : _b.gc();
        this.player = null;
        this.watcher = null;
    }
    sendObj(obj) {
        Logger_1.default.log("sendObj:", obj);
        this.connection.sendText(JSON.stringify(obj));
    }
    /**
     * 收到的message
     * isRoom   是否是房间内消息
     * type     data类型
     * data     数据
     */
    onMessage(result) {
        var _a;
        Logger_1.default.log("onMessage: " + result);
        let obj = JSON.parse(result);
        if (obj.isGame) {
            this.room.gameMgr.reciveObj(obj, this);
        }
        else {
            switch (obj.method) {
                case "userBind":
                    this.userBind(obj);
                    break;
                case "match":
                    HallMgr_1.default.match(this);
                    break;
                case "createOrjoinRoom":
                    HallMgr_1.default.createOrjoinRoom(this, obj.data.cipher);
                    break;
                case "watchingRoom":
                    HallMgr_1.default.watchingRoom(this);
                    break;
                case "cancelMatch":
                    (_a = this.room) === null || _a === void 0 ? void 0 : _a.cancelMatch(this);
                    break;
                case "chat":
                    HallMgr_1.default.chat(obj);
                    break;
                case "admitDefeat":
                    this.room.admitDefeat(this);
                    break;
                default: console.error("webSocket 收到未知方法");
            }
        }
    }
}
exports.User = User;
